<?php

namespace app\api\logic;

use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Dingtalk;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Models\GetManageProcessByStaffIdHeaders;
use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Models\GetManageProcessByStaffIdRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;


use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Models\ListProcessInstanceIdsHeaders;
use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Models\ListProcessInstanceIdsRequest;

use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Models\GetProcessInstanceHeaders;
use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Models\GetProcessInstanceRequest;

use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Models\GrantProcessInstanceForDownloadFileHeaders;
use AlibabaCloud\SDK\Dingtalk\Vworkflow_1_0\Models\GrantProcessInstanceForDownloadFileRequest;
class AOLogic extends BaseSDKLogic
{
    private static function createClient(){
        $config = new Config([]);
        $config->protocol = "https";
        $config->regionId = "central";
        return new Dingtalk($config);
    }

    // 获取所有审批表单
    public function processList()
    {
        $client = self::createClient();
        $getManageProcessByStaffIdHeaders = new GetManageProcessByStaffIdHeaders([]);
        $getManageProcessByStaffIdHeaders->xAcsDingtalkAccessToken = $this->accessToken();
        $getManageProcessByStaffIdRequest = new GetManageProcessByStaffIdRequest([
            "userId" => "3527556324052785"
        ]);

        $res = $client->getManageProcessByStaffIdWithOptions($getManageProcessByStaffIdRequest, $getManageProcessByStaffIdHeaders, $this->runtimeOptions);
        return $res->body->result;
    }

    // 获取审批实例ID列表
    public function instanceListId($processCode,$startTime,$endTime,$nextToken = 0,$maxResults = 20)
    {

        $client = self::createClient();
        $listProcessInstanceIdsHeaders = new ListProcessInstanceIdsHeaders([]);
        $listProcessInstanceIdsHeaders->xAcsDingtalkAccessToken = $this->accessToken();
        $listProcessInstanceIdsRequest = new ListProcessInstanceIdsRequest([
            "processCode" => $processCode,
            "startTime" => $startTime,
            "endTime" => $endTime,
            "nextToken" => $nextToken,
            "maxResults" => $maxResults,
//            "userIds" => [
//                "0703064907846469"
//            ]
            "statuses" => [
                "COMPLETED"
            ]
        ]);

        $res = $client->listProcessInstanceIdsWithOptions($listProcessInstanceIdsRequest, $listProcessInstanceIdsHeaders, $this->runtimeOptions);
        return $res->body->result;
    }

    // 获取单个审批实例详情
    public function instanceDetails($processInstanceId)
    {
        $client = self::createClient();
        $getProcessInstanceHeaders = new GetProcessInstanceHeaders([]);
        $getProcessInstanceHeaders->xAcsDingtalkAccessToken = $this->accessToken();
        $getProcessInstanceRequest = new GetProcessInstanceRequest([
            'processInstanceId' => $processInstanceId
        ]);

        $res = $client->getProcessInstanceWithOptions($getProcessInstanceRequest, $getProcessInstanceHeaders,$this->runtimeOptions);
        return $res->body->result;
    }

    // 下载审批附件
    public function attachment($processInstanceId,$fileId)
    {
        $client = self::createClient();
        $grantProcessInstanceForDownloadFileHeaders = new GrantProcessInstanceForDownloadFileHeaders([]);
        $grantProcessInstanceForDownloadFileHeaders->xAcsDingtalkAccessToken = $this->accessToken();
        $grantProcessInstanceForDownloadFileRequest = new GrantProcessInstanceForDownloadFileRequest([
            "processInstanceId" => $processInstanceId,
            "fileId" => $fileId
        ]);

        $res = $client->grantProcessInstanceForDownloadFileWithOptions($grantProcessInstanceForDownloadFileRequest, $grantProcessInstanceForDownloadFileHeaders, $this->runtimeOptions);

        return $res;
    }
}